package com.mebs.model.service.vehicle;

import com.mebs.model.service.Service;
import com.mebs.model.CurrencyName;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

import org.jt.jet.util.JetUtils;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com
 * Date: Mar 14, 2006, 2:48:08 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "VEHICLE")
public class Vehicle extends Service {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "VehicleId")
    @OrderBy("driverName,driverLicense")
    private Set<Driver> drivers;

    @Column(name = "Type", nullable = false, length = 250)
    private String type;

    @Column(name = "Model", nullable = false, length = 250)
    private String model;

    @Column(name = "VIN", nullable = false, length = 250)
    private String vin;

    @Column(name = "LicensePlate", nullable = false, length = 250)
    private String licensePlate;

    @Column(name = "Price", nullable = true)
    private Double price;

    @ManyToOne(optional = true)
    @JoinColumn(name = "PriceCurrencyId")
    private CurrencyName priceCurrency;

    @Column(name = "Insurance", nullable = true, length = 1024)
    private String insurance;

    @Column(name = "StartDate", nullable = true)
    private Date startDate;

    @Column(name = "EndDate", nullable = true)
    private Date endDate;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "VehicleId")
    @OrderBy("reportedDate,fixedDate")
    private Set<VehicleRepair> repairs;

    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    private VehicleStatus status;


    /**
     * **********************
     * Getters-setters
     * ***********************
     */

    public Set<Driver> getDrivers()
    {
        return drivers;
    }

    public void setDrivers(Set<Driver> drivers)
    {
        this.drivers = drivers;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public String getModel()
    {
        return model;
    }

    public void setModel(String model)
    {
        this.model = model;
    }

    public String getVin()
    {
        return vin;
    }

    public void setVin(String vin)
    {
        this.vin = vin;
    }

    public String getLicensePlate()
    {
        return licensePlate;
    }

    public void setLicensePlate(String licensePlate)
    {
        this.licensePlate = licensePlate;
    }

    public Double getPrice()
    {
        return price;
    }

    public void setPrice(Double price)
    {
        this.price = price;
    }

    public CurrencyName getPriceCurrency()
    {
        return priceCurrency;
    }

    public void setPriceCurrency(CurrencyName priceCurrency)
    {
        this.priceCurrency = priceCurrency;
    }

    public String getInsurance()
    {
        return insurance;
    }

    public void setInsurance(String insurance)
    {
        this.insurance = insurance;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public void setStartDate(Date startDate)
    {
        this.startDate = startDate;
    }

    public Date getEndDate()
    {
        return endDate;
    }

    public void setEndDate(Date endDate)
    {
        this.endDate = endDate;
    }

    public Set<VehicleRepair> getRepairs()
    {
        return repairs;
    }

    public void setRepairs(Set<VehicleRepair> repairs)
    {
        this.repairs = repairs;
    }

    public VehicleStatus getStatus()
    {
        return status;
    }

    public void setStatus(VehicleStatus status)
    {
        this.status = status;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.VEHICLE;
    }

    @Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(drivers) &&
               JetUtils.isEmpty(repairs);
    }

    @Transient
    public String getSequenceNumber()
    {
        return String.format("VE-%s-%05d",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence()
        );
    }

}
